/****************************************************
 * Description: Controllar for 角色
 * Copyright:   Copyright (c) 2013
 * Company:     beiwaionline
 * @author      bfsu
 * @version     1.0
 * @see
	HISTORY
	*  2013-08-08 bfsu Create File
**************************************************/

package com.bfsuolframework.security.controllar;

import java.util.List;

import com.bfsuolframework.security.SecurityContext;
import com.bfsuolframework.security.annotations.SecDelete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bfsuolcomponents.log.BfsuolLogger;
import com.bfsuolframework.security.annotations.SecCreate;
import com.bfsuolframework.security.annotations.SecEdit;
import com.bfsuolframework.security.annotations.SecFunction;
import com.bfsuolframework.security.annotations.SecList;
import com.bfsuolframework.security.annotations.SecPrivilege;
import com.bfsuolframework.security.entity.Role;
import com.bfsuolframework.security.entity.dto.TreeNode;
import com.bfsuolframework.security.service.RoleService;
import com.bfsuolframework.core.BfsuolConstants;
import com.bfsuolframework.core.controller.SpringControllerSupport;
import com.bfsuolframework.core.controller.json.MessageJson;
import com.bfsuolframework.core.controller.support.Pagination;
import com.bfsuolframework.core.controller.support.QueryParam;
import com.bfsuolframework.core.exception.ValidationException;
import com.bfsuolframework.core.spring.QueryParameter;
import com.bfsuolframework.core.utils.StringUtils;

@Controller
@RequestMapping("/components/security/role")
public class RoleController extends SpringControllerSupport {

    @Autowired
    private RoleService roleService;
    
    //@SecPrivilege(title="角色管理")
	@RequestMapping("/index")
	public String index(Model model){
		return getViewNamedPath("index");
	}
	
    //@SecList
	@RequestMapping("/list")
	public String list(
			@QueryParameter("query") QueryParam query,
			@ModelAttribute("page") Pagination page){
		
		page = roleService.getPage(query, page);
		
		return getViewNamedPath("list");
	}
	
    //@SecCreate
	@RequestMapping("/input")
	public String create(@ModelAttribute Role role,Model model){
		return getViewNamedPath("input");
	}
	
    //@SecEdit
	@RequestMapping("/input/{id}")
	public String edit(@PathVariable("id") Long id, Model model){
		Role role = roleService.getById(id);
		model.addAttribute(role);
		return getViewNamedPath("input");
	}
	//@SecCreate
	@SecEdit
	@RequestMapping("/save")
	public @ResponseBody MessageJson save(@ModelAttribute Role role){
		if(StringUtils.isBlank(role.getTitle())) throw new ValidationException("请填写角色名称！");
		if(StringUtils.isBlank(role.getCode())) throw new ValidationException("请填写角色代码！");
		if(!roleService.checkCode(role)) throw new ValidationException("角色代码已存在！");
		
		if(role.isNew()){
			role.setIsEnabled(BfsuolConstants.GLOBAL_YESNO_YES);// 是否可用
			role.setIsSys(BfsuolConstants.GLOBAL_YESNO_NO);// 是否系统角色
			
			roleService.save(role);
			BfsuolLogger.data("新增参赛选手，", role.getTitle());
		}else{
			Role newRole = new Role();
			newRole = roleService.getById(role.getId());
			newRole.setTitle(role.getTitle());
			newRole.setCode(role.getCode());
			newRole.setDescription(role.getDescription());
			
			roleService.update(newRole);
			BfsuolLogger.data("修改参赛选手，", role.getTitle());
		}
		return MessageJson.information("保存成功");
	}
	
	//@SecDelete
	@RequestMapping("/delete/{id}")
	public @ResponseBody MessageJson delete(@PathVariable("id") Long id){
		String res = roleService.checkDel(id);
		if("suc".equals(res)){
			roleService.delete(id);
			return MessageJson.information("成功删除1条");
		}else{
			throw new ValidationException(res);
		}
	}
	//@SecDelete
	@RequestMapping("/delete")
	public @ResponseBody MessageJson delete(@RequestParam("ids") Long[] ids){
		return MessageJson.information(roleService.deleteAll(ids));
	}

	//@SecFunction(title="设置权限",code="listpri")
	@RequestMapping("/listpri/{roleId}")
	public String listpri(@PathVariable("roleId") Long roleid, Model model){
		List<TreeNode> treeNodeList = roleService.listpri(roleid);
		model.addAttribute("roleid",roleid);
		model.addAttribute(treeNodeList);
		return getViewNamedPath("sec");
	}

	//@SecFunction(title="设置权限",code="listpri")
	@RequestMapping("/savepri")
	@ResponseBody
	public MessageJson savepri(@RequestParam("roleId") Long roleid,@RequestParam(value = "codes") String[] allCodes){
		StringBuffer bufferCodes = new StringBuffer();
		String codes = ""; 
		if(allCodes != null && allCodes.length > 0){
			for(String code:allCodes){
				bufferCodes.append(code+"%%%%");
			}
			codes = bufferCodes.toString().substring(0,bufferCodes.length()-4);
		}
		String res = roleService.savepri(roleid, codes);
		Role role=roleService.getById(roleid);
		SecurityContext.reloadRolePrivileges(role.getCode());
		SecurityContext.reloadRoleMenus(role.getCode());
		if("suc".equals(res)){
			return MessageJson.information("设置角色权限成功!");
		}else{
			throw new ValidationException(res);
		}
	}
	
}
